package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.constants.Constants;
import com.moli.iov.admin.model.vo.req.QueryTerminalInstallationAuditListParamVo;
import com.moli.iov.admin.model.vo.req.QueryVehicleTboxListParamVo;
import com.moli.iov.admin.model.vo.req.TerminalBindingDeleteVo;
import com.moli.iov.admin.model.vo.req.TerminalBindingSaveVo;
import com.moli.iov.admin.model.vo.req.TerminalInstallLogsReqVo;
import com.moli.iov.admin.model.vo.req.TerminalInstallationAuditReqVo;
import com.moli.iov.admin.service.CompanyVehicleModelService;
import com.moli.iov.admin.service.ITerminalInstallationAuditService;
import com.moli.iov.base.model.dto.req.QueryTerminalInstallationAuditListParamDto;
import com.moli.iov.base.model.dto.req.QueryVehicleTboxListParamDto;
import com.moli.iov.base.model.dto.req.TerminalBindingDeleteDto;
import com.moli.iov.base.model.dto.req.TerminalBindingSaveDto;
import com.moli.iov.base.model.dto.req.TerminalInstallLogsReqDto;
import com.moli.iov.base.model.dto.req.TerminalInstallationAuditReqDto;
import com.moli.iov.base.model.dto.res.TerminalInstallationAuditResDto;
import com.moli.iov.base.model.dto.res.VehicleTboxBindingResDto;
import com.moli.iov.base.model.mongo.TerminalInstallLogs;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备管理> 终端安装管理.
 * 终端安装管理
 * @author wanghualin
 * @date 2020 -02-25 11:05:04
 */
@Slf4j
@RestController
@RequestMapping("/equipmentManagement/install")
public class TerminalInstallationAuditController extends BaseController {


    @Autowired
    private ITerminalInstallationAuditService auditService;

    /**
     * 设备管理> 终端安装管理
     * 授权
     */
    @PostMapping("/auth/list")
    public RestResponse listVehicleTboxBinding(
            @RequestBody QueryVehicleTboxListParamVo vo) {
        assembleUserInfo(vo);
        QueryVehicleTboxListParamDto dto = new QueryVehicleTboxListParamDto();
        CommonUtils.copyProperties(vo, dto);
        return auditService
                .listVehicleTboxBinding(dto);
    }
    /**
     * 设备管理> 终端安装管理
     * 授权
     */
    @PostMapping("/auth/export")
    public RestResponse listVehicleTboxBindingExport(
            @RequestBody QueryVehicleTboxListParamVo vo, HttpServletRequest request,
            HttpServletResponse response) {
        assembleUserInfo(vo);
        QueryVehicleTboxListParamDto dto = new QueryVehicleTboxListParamDto();
        CommonUtils.copyProperties(vo, dto);
        dto.setPageNum((long) 1);
        dto.setPageSize(Constants.EXPORT_PAGE_SIZE);
        RestResponse<PageExtend<VehicleTboxBindingResDto>> restResponse = auditService
                .listVehicleTboxBinding(dto);

        if (restResponse==null||!restResponse.isFlag()) {
            return RestResponse.failure(restResponse.getMessage());
        }
        if (restResponse.getData() == null || restResponse.getData().getRecords().isEmpty()) {
            return RestResponse.failure("数据为空");
        }
        String[] propNames = {"deviceId", "iccid", "deviceModel", "vin",
                "companyName", "registerTime", "modifiedRealName"};
        String[] headStrs = {"任务稿件名称", "发布媒体", "发布频道", "合作方式", "推送时间", "稿件发布时间", "任务状态","返回信息"};
        String title = "任务状态";
        List data = restResponse.getData().getRecords();
        try {
            ExcelUtil.exportDataExcel(request, response, data, propNames, headStrs, title);
            return RestResponse.success();
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failure("导出异常");
        }
    }

    /**
     * 设备管理> 终端安装管理-添加绑定关系
     * 授权
     */
    @PostMapping("/auth/save")
    public RestResponse listVehicleTboxBindingSave(
              @RequestBody TerminalBindingSaveVo vo) {
        assembleUserInfo(vo);
        TerminalBindingSaveDto dto = new TerminalBindingSaveDto();
        CommonUtils.copyProperties(vo, dto);
        return auditService
                .listVehicleTboxBindingSave(dto);
    }
    /**
     * 设备管理> 终端安装管理-删除绑定关系
     * 授权
     */
    @PostMapping("/auth/delete")
    public RestResponse listVehicleTboxBindingDelete(
            @RequestBody TerminalBindingDeleteVo vo) {
        assembleUserInfo(vo);
        TerminalBindingDeleteDto dto = new TerminalBindingDeleteDto();
        CommonUtils.copyProperties(vo, dto);
        return auditService
                .listVehicleTboxBindingDelete(dto);
    }
    /**
     * 设备管理> 终端安装管理-添加绑定关系
     * 授权
     */
    @PostMapping("/list")
    public RestResponse listTerminalInstallationAudit(
            @RequestBody QueryTerminalInstallationAuditListParamVo vo) {
        assembleUserInfo(vo);
        QueryTerminalInstallationAuditListParamDto dto = new QueryTerminalInstallationAuditListParamDto();
        CommonUtils.copyProperties(vo, dto);
        return auditService
                .listTerminalInstallationAudit(dto);
    }
    /**
     * 设备管理> 终端安装管理-添加绑定关系
     * 授权
     */
    @PostMapping("/export")
    public RestResponse listTerminalInstallationAuditExport(
            @RequestBody QueryTerminalInstallationAuditListParamVo vo,HttpServletRequest request,
            HttpServletResponse response) {
        assembleUserInfo(vo);
        QueryTerminalInstallationAuditListParamDto dto = new QueryTerminalInstallationAuditListParamDto();
        CommonUtils.copyProperties(vo, dto);
        RestResponse<PageExtend<TerminalInstallationAuditResDto>> restResponse = auditService
                .listTerminalInstallationAudit(dto);
        if (restResponse==null||!restResponse.isFlag()) {
            return RestResponse.failure(restResponse.getMessage());
        }
        if (restResponse.getData() == null || restResponse.getData().getRecords().isEmpty()) {
            return RestResponse.failure("数据为空");
        }
        String[] propNames = {"deviceId", "iccid", "deviceModel", "vin",
                "companyName", "registerTime"};
        String[] headStrs = {"T-BoxID", "ICCID", "终端型号", "车架号", "所属公司", "授权时间"};
        String title = "安装授权";
        List data = restResponse.getData().getRecords();
        try {
            ExcelUtil.exportDataExcel(request, response, data, propNames, headStrs, title);
            return RestResponse.success();
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failure("导出异常");
        }
    }
    /**
     * 设备管理> 终端安装管理-审核
     * 授权
     */
    @PostMapping("/audit")
    public RestResponse audit(
            @Validated()  @RequestBody TerminalInstallationAuditReqVo vo) {
        assembleUserInfo(vo);
        TerminalInstallationAuditReqDto dto = new TerminalInstallationAuditReqDto();
        CommonUtils.copyProperties(vo, dto);
        return auditService
                .audit(dto);
    }
    /**
     * 设备管理> 终端安装管理-审核
     *
     */
    @PostMapping("/logs/list")
    public RestResponse listTerminalInstallLogs(
            @Validated()  @RequestBody TerminalInstallLogsReqVo vo) {
        assembleUserInfo(vo);
        TerminalInstallLogsReqDto dto = new TerminalInstallLogsReqDto();
        CommonUtils.copyProperties(vo, dto);
        return auditService
                .listTerminalInstallLogs(dto);
    }
    /**
     * 设备管理> 终端安装管理-审核
     *
     */
    @PostMapping("/logs/export")
    public RestResponse listTerminalInstallLogs(
            @Validated()  @RequestBody TerminalInstallLogsReqVo vo, HttpServletRequest request,
            HttpServletResponse response) {
        assembleUserInfo(vo);
        TerminalInstallLogsReqDto dto = new TerminalInstallLogsReqDto();
        CommonUtils.copyProperties(vo, dto);
        RestResponse<PageExtend<TerminalInstallLogs>> restResponse = auditService
                .listTerminalInstallLogs(dto);

        if (restResponse==null||!restResponse.isFlag()) {
            return RestResponse.failure(restResponse.getMessage());
        }
        if (restResponse.getData() == null || restResponse.getData().getRecords().isEmpty()) {
            return RestResponse.failure("数据为空");
        }
        String[] propNames = {"deviceId", "iccid", "deviceModel", "vin",
                "companyName", "authorizedSourceStr","authorizedResultStr","gmtCreate","createRealName"};
        String[] headStrs = {"T-BoxID", "ICCID", "终端型号", "车架号", "所属公司", "授权来源","授权结果","最后操作时间","最后操作人"};
        String title = "安装授权";
        List data = restResponse.getData().getRecords();
        try {
            ExcelUtil.exportDataExcel(request, response, data, propNames, headStrs, title);
            return RestResponse.success();
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failure("导出异常");
        }
    }

}
